#include "DaterRequestCenter.h"

namespace Dater
{
	template<> RequestCenter* Singleton<RequestCenter>::ms_Singleton = 0;

	RequestCenter::RequestCenter(): _delimiter('\b'), _selectResultDelimiter('\t'), 
		linktosql(),_user("root"),_password("19920418")
	{
		linktosql.ConnMySQL("localhost", "3306", "dater", (char*)_user.c_str(), (char*)_password.c_str() , "GBK", "");

		std::string sqlResult = linktosql.SelectData("select uid from user;", 1, linktosql.Msg, _selectResultDelimiter, _delimiter);
		linktosql.CloseMySQLConn();

		std::vector<std::string> dividedSqlResult;

		split(sqlResult, _selectResultDelimiter, dividedSqlResult);
		for(unsigned index1 = 0; index1 < dividedSqlResult.size(); index1++)
		{
			std::vector<std::string> temMessageVector;
			_requestCenterMessage[atoi(dividedSqlResult[index1].c_str())] = temMessageVector;
		}
		linktosql.CloseMySQLConn();
	}
		
	void RequestCenter::addUser(int uid)
	{
		if(_requestCenterMessage.find(uid) == _requestCenterMessage.end())
		{
			std::vector<std::string> temMessageVector;
			_requestCenterMessage[uid] = temMessageVector;
		}
	}

	void RequestCenter::pushRequestCenterMessage(int uid, std::string msg)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
			_requestCenterMessage[uid].push_back(msg);
		}
	}

	void RequestCenter::popRequestCenterMessage(int uid)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
			if(_requestCenterMessage[uid].size() != 0)
			{
				_requestCenterMessage[uid].erase(_requestCenterMessage[uid].begin());
			}
		}
	}

	void RequestCenter::clearRequestCenterMessage(int uid)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
			_requestCenterMessage[uid].clear();
		}
	}

	bool RequestCenter::isRequestCenterMessageEpmty(int uid)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
			return _requestCenterMessage[uid].empty();
		}else
			return true;
	}

	std::string RequestCenter::getRequestCenterMessage(int uid)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
			if(_requestCenterMessage[uid].size() != 0)
				return _requestCenterMessage[uid].front();
		}

		return "";
	}

	void RequestCenter::split(std::string const& order, char delimiter, std::vector<std::string>& splitedVector)
	{
		splitedVector.clear();

		if(order.size() == 0)
			return;

		std::string::size_type pos = 0, pre_pos = 0;

		while((pos = order.find_first_of(delimiter, pos)) != std::string::npos)
		{
			splitedVector.push_back(order.substr(pre_pos, pos - pre_pos));
			pre_pos = ++pos;
		}

		splitedVector.push_back(order.substr(pre_pos, pos - pre_pos));

	}

	std::vector<std::string>& RequestCenter::getMessageVector(int uid)
	{
		if(_requestCenterMessage.find(uid) != _requestCenterMessage.end())
		{
				return _requestCenterMessage[uid];
		}
		std::vector<std::string> emptyResult;
		return emptyResult;
	}
}